using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class JoystickManager : MonoBehaviour
{
    public PlayerController _playerController;

    #region 摇杆

    private float pointDis;

    public Image lookArea;
    public Transform dot;

    public Vector2 startPos;
    public Vector2 clickPos;

    public void InitJoystick()
    {
        pointDis = Screen.height * 1.0f / 1080 * Constants.ScreenOPDistance;

        rotX = _playerController.transform.eulerAngles.x;
        rotY = _playerController.transform.eulerAngles.y;
        rotX = Mathf.Clamp(360 - rotX % 360, -80, 80);
        Quaternion rotation = Quaternion.Euler(rotX, rotY, 0.0f);
        _playerController.transform.rotation = rotation;
    }

    public void OnTouchAreaClickDown(BaseEventData baseData)
    {
        startPos = dot.transform.position;
        PointerEventData eventData = baseData as PointerEventData;
        clickPos = eventData.position;

        Vector2 direction = eventData.position - startPos;
        float length = direction.magnitude;

        if (length > pointDis)
        {
            Vector2 clampDir = Vector2.ClampMagnitude(direction, pointDis);
            dot.transform.position = startPos + clampDir;
            _playerController.Dir = new Vector3(direction.x, 0, direction.y);
        }
        else
        {
            Vector2 clampDir = Vector2.ClampMagnitude(direction, pointDis);
            dot.transform.position = startPos + clampDir;
        }
    }

    public void OnTouchAreaClickUp(BaseEventData baseData)
    {
        PointerEventData eventData = baseData as PointerEventData;
        dot.transform.localPosition = Vector3.zero;
        _playerController.Dir = Vector3.zero;
    }

    public void OnTouchAreaDrag(BaseEventData baseData)
    {
        PointerEventData eventData = baseData as PointerEventData;

        Vector2 direction = eventData.position - startPos;
        dot.transform.localPosition = Vector3.zero;
        float length = direction.magnitude;

        if (length > pointDis)
        {
            Vector2 clampDir = Vector2.ClampMagnitude(direction, pointDis);
            dot.transform.position = startPos + clampDir;
            _playerController.Dir = new Vector3(direction.x, 0, direction.y);
        }
        else
        {
            Vector2 clampDir = Vector2.ClampMagnitude(direction, pointDis);
            dot.transform.position = startPos + clampDir;
        }
    }

    #region 视角旋转

    private float rotX;
    private float rotY;

    private Vector2 lookStartPos;

    public void OnLookClickDown(BaseEventData data)
    {
        PointerEventData eventData = data as PointerEventData;
        lookStartPos = eventData.position;
    }

    public void OnLookAreaDrag(BaseEventData baseData)
    {
        PointerEventData eventData = baseData as PointerEventData;
        Vector2 distance = eventData.position - lookStartPos;
        lookStartPos = eventData.position;
        rotY += distance.x * 5 * Time.deltaTime;
        rotX -= distance.y * 5 * Time.deltaTime;
        rotX = Mathf.Clamp(rotX, -80, 80);
        Quaternion rotation = Quaternion.Euler(rotX, rotY, 0.0f);
        _playerController.transform.rotation = rotation;
    }

    #endregion

    #endregion
}